import time
import allure
from config.setting import host_port
from pages.basePage2 import BasePage

#页面元素定位类
class HistoryOrderPage(BasePage):
    def __init__(self):
        BasePage.__init__(self)
        time.sleep(2)
        self.driver.get(f"http://{host_port}/SharedParkingPlace/lessor/index.html")

    #定位 历史订单按钮
    def get_historyorder_element(self):
        return self.driver.find_element_by_xpath('//ul[@class="nav nav-pills nav-justified"]/li[2]/a')

    #定位 表单
    def get_frame_element(self):
        return self.driver.find_element_by_xpath('//iframe[@name="testIframe"]')

    #定位 查询方式下拉框
    def get_querytype_element(self):
        return self.driver.find_element_by_xpath('//div[@class="page-header"]/div[2]/select[1]')

    #定位 日期选择框
    def get_datatime_element(self):
        return self.driver.find_element_by_id('time')

    #定位 状态下拉框
    def get_status_element(self):
        return self.driver.find_element_by_id('status')

    #定位 搜索按钮
    def get_query_element(self):
        return self.driver.find_element_by_xpath('//div[@class="page-header"]/div[2]/button')

    #定位 评论按钮
    def get_comment_element(self):
        return self.driver.find_element_by_xpath('//table[@class="table table-striped table-hover table-bordered"]//tr[2]/td[6]/button[2]')

    #定位 评论输入框
    def get_inputcomment_element(self):
        return self.driver.find_element_by_class_name('mytextarea')

    #定位  发送按钮
    def get_send_element(self):
        return self.driver.find_element_by_xpath('//div[@class="commmentbox"]/div')

    #定位 取消订单按钮
    def get_concel_element(self):
        return self.driver.find_element_by_xpath('//table[@class="table table-striped table-hover table-bordered"]//tr[2]//button[3]')

    #定位 车牌
    def get_licenses_element(self):
        return self.driver.find_elements_by_xpath('//table[@class="table table-striped table-hover table-bordered"]//tr/td[2]')

#操作层
class HistoryOrderHandle(HistoryOrderPage):
    def __init__(self):
        HistoryOrderPage.__init__(self)

    @allure.step('点击历史订单按钮')
    def click_historyorder(self):
        self.get_historyorder_element().click()

    @allure.step('切换表单')
    def switch_frame(self):
        frame_element = self.get_frame_element()
        self.driver.switch_to.frame(frame_element)

    @allure.step('选择查询方式并输入对应条件:日期/状态')
    def select_querytype(self, type, value):
        self.select_option(self.get_querytype_element(), 3, type)
        time.sleep(2)
        if type == '按状态查询':
            self.select_option(self.get_status_element(), 3, value)
        elif type == '按时间查询':
            self.get_datatime_element().send_keys(value)

    @allure.step('点击搜索按钮')
    def click_query(self):
        self.get_query_element().click()

    @allure.step('点击 评论按钮')
    def click_comment(self):
        self.get_comment_element().click()

    @allure.step('输入评论')
    def input_inputcomment(self, content):
        time.sleep(0.5)
        self.get_inputcomment_element().send_keys(content)

    @allure.step('点击发送按钮')
    def click_send(self):
        self.get_send_element().click()

    @allure.step('点击取消按钮')
    def click_concel(self):
        self.get_concel_element().click()

    @allure.step('获取车牌号列表')
    def get_licenses(self):
        license_elements =self.get_licenses_element()
        license_list = []
        for lic_element in license_elements:
            license_list.append(lic_element.text)
        return license_list

#业务层
class HistoryOrderBusiness(HistoryOrderHandle):
    def __init__(self):
        HistoryOrderHandle.__init__(self)

    #订单评论：历史订单-查询方式-时间/状态-搜索-评论-发送
    def comment(self, select_type, value, content):
        """
        :param select_type: 查询方式
        :param value: 状态值/时间值
        :param content: 内容
        """
        time.sleep(1)
        #点击历史订单按钮
        self.click_historyorder()
        #切换表单
        self.switch_frame()
        #选择查询方式并选择具体值
        self.select_querytype(select_type, value)
        #点击 搜索按钮
        self.click_query()
        #点击 评论按钮
        self.click_comment()
        #输入 评论内容
        self.input_inputcomment(content)
        #点击 发送按钮
        self.click_send()
        #获取 弹窗文本
        time.sleep(1)
        alertobj = self.driver.switch_to.alert
        r = alertobj.text
        alertobj.accept()
        return r

    #订单取消：历史订单-切换表单-按状态查询-进行中-搜索-取消-确定
    def concenl_Order(self):

        #点击 历史订单按钮
        self.click_historyorder()
        # 切换表单
        self.switch_frame()
        #点击 按状态查询并选择状态
        self.select_querytype('按状态查询', '进行中')
        #点击 搜索按钮
        self.click_query()
        time.sleep(0.5)
        #获取 取消前订单
        orderlist = self.get_licenses()
        #点击 取消按钮
        self.click_concel()
        #点击 confirm弹窗确定按钮
        alertObject = self.driver.switch_to.alert
        alertObject.accept()
        #获取 取消后订单
        self.select_querytype('按状态查询', '进行中')
        self.click_query()
        time.sleep(0.5)
        neworderlist = self.get_licenses()
        return orderlist,neworderlist

if __name__ == "__main__":
    h1 = HistoryOrderBusiness()
    # r = h1.comment('按状态查询','进行中','测试专用评论4')
    # r = h1.comment('按时间查询', '002021-06-28', '测试专用评论3')
    r,n = h1.concenl_Order()
    print(r,n)